package com.tarena.lbs.basic.web.dao.repository.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.base.protocol.pager.BasePageList;
import com.tarena.lbs.basic.web.dao.mapper.AdminMapper;
import com.tarena.lbs.basic.web.dao.repository.AdminRepository;
import com.tarena.lbs.common.basic.converter.AdminConverter;
import com.tarena.lbs.pojo.basic.bo.AdminBO;
import com.tarena.lbs.pojo.basic.dos.AdminDO;
import com.tarena.lbs.pojo.basic.query.AdminQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Repository
public class AdminRepositoryImpl implements AdminRepository {

    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private AdminConverter adminConverter;

    @Override
    public AdminBO getUserByPhone(String phone) {
        log.debug("通过手机号查询User信息入参 phone:{}",phone);
        AdminDO adminDO = adminMapper.selectAdminByPhone(phone);
        AdminBO adminBO = new AdminBO();
        //转化为BO
        if (adminDO!=null){
            BeanUtils.copyProperties(adminDO,adminBO);
            log.debug("通过手机号查询User信息出参:adminBO:{}",adminBO);
        }
        return adminBO;
    }

    @Override
    public AdminBO getAdminById(Integer userId) {
        //1. 利用id查询 adminDO
        AdminDO adminDO=adminMapper.selectAdminById(userId);
        log.debug("查询selectAdminById返回的数据:adminDO:{}",adminDO);
        //2. 转化返回 考虑空
        AdminBO adminBO=null;
        if (adminDO!=null){
            adminBO=new AdminBO();
            BeanUtils.copyProperties(adminDO,adminBO);
            log.debug("查询getAdminById赋值后的数据:{}",adminBO);
        }
        return adminBO;
    }

    @Override
    public BasePageList<AdminBO> adminList(AdminQuery query) {
        log.debug("查询商家的分页信息参数 AdminQuery:{}",query);
        //1.开启分页查询
        PageHelper.startPage(query.getPageNo(), query.getPageSize());
        //2.保证线程安全 startPage下一步必须是sql查询搜 pageHelper线程安全问题博文
        List<AdminDO> adminDOS= adminMapper.selectAdminByPage(query);
        //3.DO转化为BO
        PageInfo<AdminDO> pageInfo = new PageInfo<>(adminDOS);
        log.debug("转化为AdminDO对象的数据 pageInfo:{}",pageInfo);
        //在Total不为空的时候,封装一个BasePageList
        long total = pageInfo.getTotal();
        BasePageList<AdminBO> basePageList = null;
        if (total >0){
            List<AdminDO> dos = pageInfo.getList();
            List<AdminBO> bos = dos.stream().map(adminDO -> {
                AdminBO bo = new AdminBO();
                BeanUtils.copyProperties(adminDO, bo);
                return bo;
            }).collect(Collectors.toList());
            basePageList= new BasePageList<>(bos,total);
            log.debug("basePageList的数据为:{}",basePageList);
        }else {
            basePageList=new BasePageList<>(null,0L);
        }
        return basePageList;
    }

    @Override
    public long countAdminByPhone(String accountPhone) {
        return adminMapper.selectCountByPhone(accountPhone);
    }

    @Override
    public void save(AdminBO adminBO) {
        log.debug("保存商家的账号信息: AdminBO:{}",adminBO);
        AdminDO adminDO = new AdminDO();
        BeanUtils.copyProperties(adminBO,adminDO);
        adminMapper.insertAdminObj(adminDO);
    }
}
